﻿
using System;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;

using Gola.VN.Scb.Web.Bll;
using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Presentation.Models
{
    public class DataRespository
    {
        public static ProductsBll ProductService { private set; get; }

        public static void Initialize()
        {
            var ReloadDataTime = DateTime.Parse(ConfigurationManager.AppSettings["ReloadDataTime"]);
            if (ProductService == null)
            {
                ProductService = new ProductsBll(getConnectionString());
            }

            ProductService.FetchData();
            //if ((DateTime.Now.Equals(ReloadDataTime) && !ProductService.IsLoading) ||
            //    ProductService.NeedLoadData)
            //{
            //    var thread = new Thread(ProductService.FetchData);
            //    thread.Start();
            //}
            MainBiz mainBiz = MainBiz.GetInstant();
        }

        private static string getConnectionString()
        {
            string conn = ConfigurationManager.ConnectionStrings["SCB"].ToString();
            return Security.Crypto.Decrypt(conn); //todo: change to dao
        }
    }

    //#region provider
    //public class ProviderService
    //{
    //    public static IEnumerable<ProviderEntity> GetProviderAllLocations()
    //    {
    //        return DataRespository.ProductService.Providers.Select(p => GetProviderLocation(p.Id, p.Name));

    //    }

    //    public static ProviderEntity GetProviderLocation(int providerId, string providerName)
    //    {
    //        var provider = DataRespository.ProductService.Providers.FirstOrDefault(q => q.Id == providerId);
    //        provider.ProviderLocations = DataRespository.ProductService.ProviderLocations.Where(q => q.ProviderId == providerId);
    //        return provider;
    //    }

    //    public static int Insert(ProviderEntity provider)
    //    {
    //        return DataRespository.ProductService.InsertProvider(provider);
    //    }

    //    public static int Update(ProviderEntity provider)
    //    {
    //        return DataRespository.ProductService.UpdateProvider(provider);
    //    }

    //    public static ProviderEntity GetProvider(string name)
    //    {
    //        var providers = DataRespository.ProductService.Providers;
    //        return providers.FirstOrDefault(p => p.Name == name && p.ExpressionId == ProductsBll.VirtualExpressionId);
    //    }

    //    public static ProviderEntity GetProvider(int id)
    //    {
    //        var providers = DataRespository.ProductService.Providers;
    //        return providers.FirstOrDefault(p => p.Id == id);
    //    }
    //    public static List<ProviderEntity> GetCustomers()
    //    {
    //        IEnumerable<ProviderEntity> providers = DataRespository.ProductService.Providers;

    //        return (from p in providers where p.ExpressionId == ProductsBll.VirtualExpressionId select p).ToList();
    //    }
    //}
    //#endregion

    //#region search history
    //public class SearchHistoryService
    //{
    //    private static Dictionary<string, SearchHistoryEntity> _searchHistories;

    //    private static void UpdateSearchHistories()
    //    {
    //        if (_searchHistories == null || _searchHistories.Count() < 0)
    //        {
    //            _searchHistories = new Dictionary<string, SearchHistoryEntity>();
    //            List<SearchHistoryEntity> searchHistories = DataRespository.ProductService.Searchhistories;
    //            if (searchHistories != null)
    //            {
    //                foreach (SearchHistoryEntity dtoSearchHistory in searchHistories)
    //                {
    //                    if (_searchHistories.ContainsKey(dtoSearchHistory.Text))
    //                        _searchHistories[dtoSearchHistory.Text].Count += dtoSearchHistory.Count;
    //                    else
    //                        _searchHistories.Add(dtoSearchHistory.Text, dtoSearchHistory);
    //                }
    //            }
    //        }
    //    }

    //    public static List<SearchHistoryEntity> GetSearchHistories()
    //    {
    //        UpdateSearchHistories();

    //        return _searchHistories.Values.ToList<SearchHistoryEntity>();
    //    }

    //    public static void UpdateSearchHistory(SearchHistoryEntity dtoSearchHistory)
    //    {
    //        UpdateSearchHistories();
    //        DataRespository.ProductService.UpdateSearchHistory(dtoSearchHistory);

    //        if (_searchHistories.ContainsKey(dtoSearchHistory.Text))
    //            _searchHistories[dtoSearchHistory.Text].Count += dtoSearchHistory.Count;
    //        else
    //            _searchHistories.Add(dtoSearchHistory.Text, dtoSearchHistory);
    //    }
    //}
    //#endregion
}